package test.my.com.myapplicationtest.pull_scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ScrollView;
import android.widget.Scroller;

/**
 * Author : Cooper
 * Time : 2016/6/18  14:55
 * Description :
 */
public class PullScrollView extends ScrollView {

    private Context mContext;
    private Scroller mScroller;
    private int mScreenHeight;
    public PullScrollView(Context context) {
        super(context);
        init(context);
    }

    public PullScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PullScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        mScroller = new Scroller(context);
        this.mContext = context;
        mScreenHeight = getScreenHeight(context) - 30;
    }

    public void smoothScrolltoo(int startY,int y){
        mScroller.startScroll(0,startY,0,y,500);
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            smoothScrollTo(mScroller.getCurrX(),mScroller.getCurrY());
            postInvalidate();
        }
    }

    // 分别记录上次滑动的坐标
    private int mLastX = 0;
    private int mLastY = 0;

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastX;
                int deltaY = y - mLastY;
                scrollBy(-deltaY, 0);
                break;
            case MotionEvent.ACTION_UP:
                int scrollY = getScrollY();// 定点距离一开始位置的距离
                Log.e("ah","getScrollY === "+y);
                int scrollDistance = 0;
                if(scrollY < mScreenHeight/2){
                    scrollDistance = 1;
                    smoothScrolltoo(scrollY,scrollDistance-scrollY);
                }else if(scrollY > mScreenHeight/2){
                    scrollDistance = mScreenHeight;
                    smoothScrolltoo(scrollY,scrollDistance-scrollY);
                }
                break;
        }
        mLastX = x;
        mLastY = y;
        return super.onTouchEvent(ev);
    }

    /**
     * 获取屏幕高度
     * @param context
     * @return
     */
    public int getScreenHeight(Context context) {
        return ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getHeight();
    }
}
